﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DataClassLayer
{
    public class CountryCollection
    {
        public CountryCollection()
        {
            
        }
        public bool Add(Country country)
        {
            try
            {
                List<string> query = new List<string>();
                string command = @"Insert into SOCCER_COUNTRY (Name,Description) Values
                   ('" + country.Name + "','"
                   + country.Description + "')";
                query.Add(command);
                DataAccessObject.DataBase.CreateUpdateDelete(query);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool Edit(Country country)
        {
            try
            {
                List<string> query = new List<string>();
                string command = @"Update SOCCER_COUNTRY Set 
                     Name='" + country.Name +
                     "',Descriptionh='" + country.Description  + "' Where ID=" + country.ID;
                query.Add(command);
                DataAccessObject.DataBase.CreateUpdateDelete(query);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool Delete(Country country)
        {
            try
            {
                List<string> query = new List<string>();
                string command = @"Delete From SOCCER_COUNTRY Where ID= " + country.ID; 
                query.Add(command);
                DataAccessObject.DataBase.CreateUpdateDelete(query);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public DataTable FillList(Country country)
        {
            try
            {
                string wherestatement = WhereStatement(country);
                string command = @"SELECT        ID, Name, Description
FROM            SOCCER_COUNTRY";
                if (wherestatement != null &&
                    wherestatement != "")
                {
                    command += " Where " + wherestatement;
                }
                DataTable table = DataAccessObject.DataBase.ExcuteQuery(command);
                return table;
            }
            catch
            {
                return null;
            }
        }
        public int MaxID()
        {
            try
            {
                int Maxid = 1;
                string command = @"SELECT    Max(ID) AS MaxID FROM   SOCCER_COUNTRY";
                DataTable table = DataAccessObject.DataBase.ExcuteQuery(command);
                if (table.Rows.Count == 1)
                {
                    Maxid = int.Parse(table.Rows[0]["MaxID"].ToString());
                }
                return Maxid;
            }
            catch
            {
                return 1;
            }
        }
        public string WhereStatement(Country country)
        {
            string wherestatement = "";
            if (country.ID > 0)
            {
                if (wherestatement != null &&
                    wherestatement != "")
                {
                    wherestatement += "And ID= " + country.ID;
                }
                else
                {
                    wherestatement += "ID= " + country.ID;
                }
            }
            if (country.Name != null &&
                country.Name != "")
            {
                if (wherestatement != null &&
                    wherestatement != "")
                {
                    wherestatement += "And Name Like '%" + country.Name + "%' ";
                }
                else
                {
                    wherestatement += " Name Like '%" + country.Name + "%' ";
                }
            }
            return wherestatement;
        }
    }
}
